package action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ICity;
import model.IRouts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.CityService;
import service.RoutsService;
import service.RoutsStantionService;
import service.ServiceManager;
import form.AdminRoutsStantionForm;

public class AdminRoutsStantionAction extends ConnectionAwareAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		AdminRoutsStantionForm input =  (AdminRoutsStantionForm) form;
		
		int[] id_city =input.getId_city();
		int id_routs = input.getRouts_name();
		
		RoutsStantionService routsStantionService = ServiceManager.getDefault().getRoutsStantionService();
		RoutsService routsService = ServiceManager.getDefault().getRoutsService();
		
		CityService cityService = ServiceManager.getDefault().getCityService();
			
		if (id_city !=null && id_routs >0){
			if(request.getParameter("add")!=null){
				routsStantionService.setRoutsStantion(id_city, id_routs);
				input.reset(mapping, request);
			}
		}
		
		List<IRouts> routs = routsService.getRouts();
		request.setAttribute("routs", routs);
		
		List<ICity> citys = cityService.getCity();
		request.setAttribute("citys", citys);
	
		return mapping.findForward(ForwardActionTo.SUCCESS);
	}
}
